package com.yuanqi.controller;

import com.yuanqi.domain.PaymentApproval;
import com.yuanqi.service.PaymentApprovalService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/finance/payment-approvals")
@CrossOrigin(origins = "*")
public class PaymentApprovalController {
    
    @Autowired
    private PaymentApprovalService paymentApprovalService;
    
    @GetMapping
    public ResponseEntity<Map<String, Object>> getPaymentApprovals(
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "10") int size,
            @RequestParam(required = false) String applicant,
            @RequestParam(required = false) String approvalStatus,
            @RequestParam(required = false) String applyDate) {
        
        try {
            Pageable pageable = PageRequest.of(page - 1, size);
            
            // 调用Service查询数据
            Page<PaymentApproval> approvalPage = paymentApprovalService.getPaymentApprovals(
                    applicant, approvalStatus, applyDate, pageable);
            
            Map<String, Object> response = new HashMap<>();
            response.put("content", approvalPage.getContent());
            response.put("totalElements", approvalPage.getTotalElements());
            response.put("totalPages", approvalPage.getTotalPages());
            response.put("currentPage", page);
            response.put("size", size);
            
            return ResponseEntity.ok(response);
            
        } catch (Exception e) {
            Map<String, Object> errorResponse = new HashMap<>();
            errorResponse.put("error", "获取付款审批数据失败: " + e.getMessage());
            return ResponseEntity.badRequest().body(errorResponse);
        }
    }
    
    @PostMapping("/{id}/approve")
    public ResponseEntity<Map<String, Object>> approvePayment(@PathVariable Long id, @RequestBody Map<String, Object> approvalRequest) {
        try {
            String approver = (String) approvalRequest.get("approver");
            String approveRemark = (String) approvalRequest.get("approveRemark");
            
            // 调用Service审批付款申请
            PaymentApproval approval = paymentApprovalService.approvePayment(id, approver, approveRemark);
            
            Map<String, Object> response = new HashMap<>();
            response.put("success", true);
            response.put("message", "付款申请已通过");
            response.put("approvalId", approval.getId());
            
            return ResponseEntity.ok(response);
            
        } catch (Exception e) {
            Map<String, Object> errorResponse = new HashMap<>();
            errorResponse.put("success", false);
            errorResponse.put("message", "审批失败: " + e.getMessage());
            return ResponseEntity.badRequest().body(errorResponse);
        }
    }
    
    @PostMapping("/{id}/reject")
    public ResponseEntity<Map<String, Object>> rejectPayment(@PathVariable Long id, @RequestBody Map<String, Object> rejectionRequest) {
        try {
            String approver = (String) rejectionRequest.get("approver");
            String approveRemark = (String) rejectionRequest.get("approveRemark");
            
            // 调用Service拒绝付款申请
            PaymentApproval approval = paymentApprovalService.rejectPayment(id, approver, approveRemark);
            
            Map<String, Object> response = new HashMap<>();
            response.put("success", true);
            response.put("message", "付款申请已拒绝");
            response.put("approvalId", approval.getId());
            
            return ResponseEntity.ok(response);
            
        } catch (Exception e) {
            Map<String, Object> errorResponse = new HashMap<>();
            errorResponse.put("success", false);
            errorResponse.put("message", "拒绝失败: " + e.getMessage());
            return ResponseEntity.badRequest().body(errorResponse);
        }
    }
    
    @PostMapping("/batch-submit")
    public ResponseEntity<Map<String, Object>> batchSubmitPaymentApprovals(@RequestBody List<Map<String, Object>> approvalDataList) {
        try {
            // 调用Service批量创建付款审批
            List<PaymentApproval> approvals = paymentApprovalService.createBatchPaymentApprovals(approvalDataList);
            
            Map<String, Object> response = new HashMap<>();
            response.put("success", true);
            response.put("message", "批量提交付款申请成功");
            response.put("count", approvals.size());
            
            return ResponseEntity.ok(response);
            
        } catch (Exception e) {
            Map<String, Object> errorResponse = new HashMap<>();
            errorResponse.put("success", false);
            errorResponse.put("message", "批量提交失败: " + e.getMessage());
            return ResponseEntity.badRequest().body(errorResponse);
        }
    }
    
    @GetMapping("/stats")
    public ResponseEntity<Map<String, Object>> getPaymentApprovalStats() {
        try {
            // 调用Service获取统计信息
            Map<String, Object> stats = paymentApprovalService.getApprovalStatistics();
            return ResponseEntity.ok(stats);
            
        } catch (Exception e) {
            Map<String, Object> errorResponse = new HashMap<>();
            errorResponse.put("error", "获取统计信息失败: " + e.getMessage());
            return ResponseEntity.badRequest().body(errorResponse);
        }
    }
    
}


